/*
 * @(#)MimirPrefixMapper.java	1.0 Apr 17, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.db.mimir;

import org.eoti.spec.mimirdb.v1.DBMapping;

import java.util.*;

import com.sun.xml.bind.marshaller.NamespacePrefixMapper;

import javax.xml.bind.Marshaller;
import javax.xml.bind.JAXBException;

/**
 * This class provides a Namespace remapper so that you don't end up with ns1="blah"
 */
public class MimirPrefixMapper
/**
 * JDK5 w/ JWSDP 1.5
 * JDK7
 * JAXB 2.1.3
 */
extends com.sun.xml.bind.marshaller.NamespacePrefixMapper
/**
 * JDK6
 * JAXB 2.0ea3
 */
//extends com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper
{
    /**
     * JDK5 w/ JWSDP 1.5
     * JDK7
     * JAXB 2.1.3
     */
    private static final String PROPNAME = "com.sun.xml.bind.namespacePrefixMapper";
    /**
     * JDK6
     * JAXB 2.0ea3
     */
//    private static final String PROPNAME = "com.sun.xml.internal.bind.namespacePrefixMapper";

	protected MimirRegistry registry;
	protected String targetNamespaceURI;

	public MimirPrefixMapper(MimirRegistry registry)
	{
		this(registry, null);
	}

	public MimirPrefixMapper(MimirRegistry registry, String targetNamespaceURI)
	{
		this.registry = registry;
		this.targetNamespaceURI = targetNamespaceURI;
	}

	public List<DBMapping> getMappings()
			throws MimirException
	{
		return registry.getConfig().getMappings();
	}

	public DBMapping getMapping(String namespaceURI)
			throws MimirException
	{
		for(DBMapping mapping : getMappings())
		{
			if(mapping.getNamespaceURI().equalsIgnoreCase(namespaceURI))
				return mapping;
		}

		return null;
	}

	public String getPreferredPrefix(String namespaceURI, String suggestion, boolean requirePrefix)
	{
		if((targetNamespaceURI != null) && (!requirePrefix) && (namespaceURI != null))
		{
			if(targetNamespaceURI.equals(namespaceURI))
				return ""; // this is our targetNamespace
		}

		DBMapping mapping = null;
		try{
			mapping = getMapping(namespaceURI);
		} catch(MimirException e){
			e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
		}
		if(mapping != null)
			return mapping.getNamespacePrefix();

		return suggestion;
	}

    public void attachTo(Marshaller marshaller)
    {
        try{
            marshaller.setProperty(PROPNAME, this);
        }catch(JAXBException ignorable){
            /**
             * If JAXB doesn't recognize/accept the prefix mapping, it will throw an exception.
             * However, since being unable to provide a human friendly prefix is not a fatal error,
             * we will simply ignore it and continue
             */
        }
    }


}
